*** Stata 15 reproduction code for

*Mader, Matthias & Harald Schoen.
*What a difference an event can make: The European refugee crisis, party competition, and voters’ responses in Germany
*West European Politics


*** Figure A4-1: Effects of change in absolute distance to the CDU on change in vote choice and party identity
****************************************************************************************************************

use "DATA_wide.dta", clear

*** Figure A4-1, left panel

	*Regressions for left panel of Figure A4-1 --- Dependent variable: Moves toward and from the CDU/CSU
 	matrix w7w11 = J(4,3,.) // 4 rows, 3 columns 
 	matrix w7w13 = J(4,3,.) // 4 rows, 3 columns 
 	matrix w7w16 = J(4,3,.) // 4 rows, 3 columns

	foreach i in 11 13 16 {
	logit from7_wunion`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimate store m_from7_wunion`i'
	margins, dydx(rdiff6_adist_cdu`i' rdiff6_adist_csu`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[1, 1] = r[1,1]
	matrix w7w`i'[1, 2] = r[5,1]
	matrix w7w`i'[1, 3] = r[6,1]
	matrix list w7w`i'
	}

	foreach i in 11 13 16 {
	logit toward7_wunion`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimate store m_toward7_wunion`i'
	margins, dydx(rdiff6_adist_cdu`i' rdiff6_adist_csu`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[2, 1] = r[1,1]
	matrix w7w`i'[2, 2] = r[5,1]
	matrix w7w`i'[2, 3] = r[6,1]
	matrix list w7w`i'
	}

	
	foreach i in 11 13 16 {
	logit from7_iunion`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimate store m_from7_iunion`i'
	margins, dydx(rdiff6_adist_cdu`i' rdiff6_adist_csu`i')
	mat r=r(table)
	matrix w7w`i'[3, 1] = r[1,1]
	matrix w7w`i'[3, 2] = r[5,1]
	matrix w7w`i'[3, 3] = r[6,1]
	matrix list w7w`i'	
	}
	
	foreach i in 11 13 16 {
	logit toward7_iunion`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimate store m_toward7_iunion`i'
	margins, dydx(rdiff6_adist_cdu`i' rdiff6_adist_csu`i')
	mat r=r(table)
	matrix w7w`i'[4, 1] = r[1,1]
	matrix w7w`i'[4, 2] = r[5,1]
	matrix w7w`i'[4, 3] = r[6,1]
	matrix list w7w`i'	
	}

	*** Create Figure A4-1, left panel
	coefplot (matrix(w7w11[,1]), ci((w7w11[,2] w7w11[,3])) msymbol(circle) mcolor(edkblue) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue)) offset(0.05)) ///
			 (matrix(w7w13[,1]), ci((w7w13[,2] w7w13[,3])) msymbol(circle) mcolor(edkblue*.7) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue*.7)) offset(-.05)) ///
			 (matrix(w7w16[,1]), ci((w7w16[,2] w7w16[,3])) msymbol(circle) mcolor(edkblue*.4) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue*.4)) offset(-0.15)) ///
			,scheme(plottigblind) /// 
			coeflabels(r1="{&Delta} vote choice, away" r2="{&Delta} vote choice, toward" r3="{&Delta} ID, away" r4="{&Delta} ID, toward" ,labsize(large) labcolor(gs0)) ///
			xscale(range(-.2 .8)) xlabel(-.2 (.2) .8, noticks labsize(large) labcolor(gs0))  xmtick(none) ///
			legend(order(1 "w4-w7" 3 "w4-w8" 5 "w4-w9") position(6) col(4)  size(large) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-5 0 -5 0)) ysize(2.5) xsize(3.5)  /// scale(.8)
			title("CDU/CSU", size(large) color(gs0)) ///
			name(Fig4_1a, replace)

		
*** Figure A4-1, left panel

	*Regressions for right panel of Figure A4-1 --- Dependent variable: Moves toward and from the AfD
	
 	foreach i in 11 13 16{
	logit from7_wafd`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimate store m_from7_wafd`i'
	margins, dydx(rdiff6_adist_cdu`i' rdiff6_adist_csu`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[1, 1] = r[1,1]
	matrix w7w`i'[1, 2] = r[5,1]
	matrix w7w`i'[1, 3] = r[6,1]
	matrix list w7w`i'
	}
	foreach i in 11 13 16{
	logit toward7_wafd`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimate store m_toward7_wafd`i'
	margins, dydx(rdiff6_adist_cdu`i' rdiff6_adist_csu`i')
	matrix list r(table)
	mat r=r(table)
	matrix w7w`i'[2, 1] = r[1,1]
	matrix w7w`i'[2, 2] = r[5,1]
	matrix w7w`i'[2, 3] = r[6,1]
	matrix list w7w`i'
	}
	foreach i in 11 13 16 {
	logit from7_iafd`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimate store m_from7_iafd`i'
	margins, dydx(rdiff6_adist_cdu`i' rdiff6_adist_csu`i')
	mat r=r(table)
	matrix w7w`i'[3, 1] = r[1,1]
	matrix w7w`i'[3, 2] = r[5,1]
	matrix w7w`i'[3, 3] = r[6,1]
	matrix list w7w`i'	
	}
	foreach i in 11 13 16 {
	logit toward7_iafd`i' rdiff6_adist_cdu`i' rdiff6_adist_csu`i' rdiff6_wadist_cdu`i' rdiff6_wadist_csu`i'
	estimate store m_toward7_iafd`i'
	margins, dydx(rdiff6_adist_cdu`i' rdiff6_adist_csu`i')
	mat r=r(table)
	matrix w7w`i'[4, 1] = r[1,1]
	matrix w7w`i'[4, 2] = r[5,1]
	matrix w7w`i'[4, 3] = r[6,1]
	matrix list w7w`i'	
	}
	
	*** Create Figure A4-1, right panel
	coefplot (matrix(w7w11[,1]), ci((w7w11[,2] w7w11[,3])) msymbol(circle) mcolor(edkblue) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue)) offset(0.05)) ///
			 (matrix(w7w13[,1]), ci((w7w13[,2] w7w13[,3])) msymbol(circle) mcolor(edkblue*.7) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue*.7)) offset(-.05)) ///
			 (matrix(w7w16[,1]), ci((w7w16[,2] w7w16[,3])) msymbol(circle) mcolor(edkblue*.4) msize(large) ciopts(recast(rbar) barwidth(0.03) color(edkblue*.4)) offset(-0.15)) ///
			,scheme(plottigblind) /// 
			coeflabels(r1="{&Delta} vote choice, away" r2="{&Delta} vote choice, toward" r3="{&Delta} ID, away" r4="{&Delta} ID, toward" ,labsize(large) labcolor(gs0)) ///
			xscale(range(-.2 .8)) xlabel(-.2 (.2) .8, noticks labsize(large) labcolor(gs0)) xmtick(none) ///
			legend(order(1 "w4-w7" 3 "w4-w8" 5 "w4-w9") position(6) col(4)  size(large) symxsize(5) region(lcolor(gs16)))  /// 
			plotregion(color(gs15)) ///
			graphregion(margin(-5 0 -5 0)) ysize(2.5) xsize(3.5)  /// scale(.8)
			title("AfD", size(large) color(gs0)) ///
			name(FigA4_1b, replace)
